import java.beans.IntrospectionException;
import java.util.Scanner;

public class Main {

    //过河卒
    private  boolean[][] isHorsePoint;
    private final int[] dx = new int[]{1,1,-1,-1,2,2,-2,-2};
    private final int[] dy = new int[]{2,-2,2,-2,1,-1,1,-1};
    public static void main(String[] args) {

    }



    //买卖股票的最好时机（1）
    public static void main1(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int[] p = new int[n];
        for (int i = 0; i < n; i++) {
            p[i] = in.nextInt();
        }
        System.out.println(getMaxMoney(p));
    }
    private static int getMaxMoney(int[] p){
        int res = 0,inM = Integer.MAX_VALUE,outM = Integer.MIN_VALUE;
        for (int i = 0; i < p.length-1; i++) {
            if(p[i]>=inM){
                continue;
            }
            if(p[i]<inM){
                inM = p[i];
                outM = Integer.MIN_VALUE;
            }
            for (int j = i+1; j < p.length; j++) {
                if (p[j]<=outM){
                    continue;
                }
                if (p[j]>outM){
                    outM = p[j];
                    res  = Math.max(res,outM-inM);
                }
            }
        }
        return res;
    }



    //最长回文子串
    public int dfs(String s,int begin,int end){
        while(begin>=0 && end<s.length() && s.charAt(begin) == s.charAt(end)){
            begin--;
            end++;
        }
        return end-begin-1;
    }
    public int getLongestPalindrome(String A){
        int max = 1;
        for (int i = 0; i < A.length(); i++) {
            max = Math.max(max,Math.max(dfs(A,i,i),dfs(A,i,i+1)));
        }
        return max;
    }



}
